package com.fishery.base.controller;


import com.fishery.base.entity.vo.ProductInventoryVo;
import com.fishery.base.service.client.ProcessingFactoryClient;
import com.fishery.base.service.client.RefrigeratoryClient;
import com.fishery.base.service.*;
import com.fishery.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 水产品前端控制器
 * </p>
 *
 * @author Ghost
 * @since 2021-01-15
 */
@RestController
@RequestMapping("/base/product/order")
@Api(tags = "水产品订单管理模块")
@CrossOrigin
public class ProductOrderController {


	@Autowired
	private GermchitService germchitService;


	@Autowired
	private ProcessingFactoryClient processingFactoryClient;

	@Autowired
	private RefrigeratoryClient refrigeratoryClient;

	@Autowired
	private CatchService catchService;


	@GetMapping("/pro/{germchitId}")
	@ApiOperation("查询相关水产品的加工厂")
	public Result searchFactoryOfGermchit(@PathVariable(name = "germchitId", required = true) @ApiParam(value = "种苗Id", required = true) String germchitId) {
		if (StringUtils.isEmpty(germchitId)) {
			return Result.error("查询失败");
		}
		// 获取品种
		String species = germchitService.getSpeciesOfGermchit(germchitId);
		// 获取改品种对应的加工厂
		return processingFactoryClient.searchFactoryByType(species);
	}

	@GetMapping("/ref")
	@ApiOperation("查询冷库")
	public Result seachRefrigeratory() {
		return refrigeratoryClient.getAllRefrigeratory();
	}


	@PutMapping("/decrease")
	@ApiOperation("减少库存（前端不需要调用）")
	public Result decreaseAmountAndWeightOfRecord(@RequestBody ProductInventoryVo productInventoryVo) {
		if (productInventoryVo == null
				|| StringUtils.isEmpty(productInventoryVo.getProductId())
				|| productInventoryVo.getAmount() <= 0
				|| productInventoryVo.getWeight() <= 0) {
			return Result.error("减少库存失败");
		}
		boolean ret = catchService.decrease(productInventoryVo.getProductId(),
				productInventoryVo.getAmount(), productInventoryVo.getWeight());
		return ret ? Result.success("减少库存成功") : Result.error("减少库存失败");
	}


	@PutMapping("/increase")
	@ApiOperation("增加库存（前端不需要调用）")
	public Result increaseAmountAndWeightOfRecord(@RequestBody ProductInventoryVo productInventoryVo) {
		if (productInventoryVo == null
				|| StringUtils.isEmpty(productInventoryVo.getProductId())) {
			return Result.error("增加库存失败");
		}
		boolean ret = catchService.increase(productInventoryVo.getProductId(),
				productInventoryVo.getAmount(), productInventoryVo.getWeight());
		return ret ? Result.success("增加库存成功") : Result.error("增加库存失败");
	}

}

